<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库管理</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script src="js/page.js"></script>
<script type="text/javascript">
<!--
function mouseOverHandler(record_index)
{
	if (record_index == "[empty]")
		document.getElementById("tr_[empty]").style.backgroundColor = "#FFFFFF";
	else if (document.getElementById("checkbox_" + record_index).checked == true)
		document.getElementById("tr_" + record_index).style.backgroundColor = "#FFFFC1";
	else
		document.getElementById("tr_" + record_index).style.backgroundColor = "#FFFFE1";
}

function mouseOutHandler(record_index)
{
	if (record_index == "[empty]")
		document.getElementById("tr_[empty]").style.backgroundColor = "#FFFFFF";
	else if (document.getElementById("checkbox_" + record_index).checked == true)
		document.getElementById("tr_" + record_index).style.backgroundColor = "#F4F4F4";
	else
		document.getElementById("tr_" + record_index).style.backgroundColor = "#FFFFFF";
}

function clickHandler(record_index)
{
	if (record_index == "[empty]")
		return;
	document.getElementById("checkbox_" + record_index).checked = !document.getElementById("checkbox_" + record_index).checked;
	if (document.getElementById("checkbox_" + record_index).checked == true)
		document.getElementById("tr_" + record_index).style.backgroundColor = "#FFFFC1";
	else
		document.getElementById("tr_" + record_index).style.backgroundColor = "#FFFFE1";
	var choose_all = true;
	for (var i = 0; i < document.getElementsByName("checkbox").length; i++)
	{
		if (document.getElementsByName("checkbox").item(i).checked == false)
		{
			choose_all = false;
			break;
		}
	}
	if (choose_all == true)
		document.getElementById("choose_all").checked = true;
	else
		document.getElementById("choose_all").checked = false;
}

function chooseAllHandler()
{
	var choose_all = document.getElementById("choose_all").checked;
	var checkboxes = document.getElementsByName("checkbox");
	for (var i = 0; i < checkboxes.length; i++)
	{
		checkboxes.item(i).checked = choose_all;
		if (choose_all == true)
			document.getElementById("tr_" + checkboxes.item(i).id.substring("checkbox_".length)).style.backgroundColor = "#F4F4F4";
		else
			document.getElementById("tr_" + checkboxes.item(i).id.substring("checkbox_".length)).style.backgroundColor = "#FFFFFF";
	}
}

function deleteHandler()
{
	var deleted_records = "";
	var checkboxes = document.getElementsByName("checkbox");
	for (var i = 0; i < checkboxes.length; i++)
	{
		if (checkboxes.item(i).checked == true)
			deleted_records = deleted_records + (parseInt(checkboxes.item(i).id.substring("checkbox_".length)) + 1) + ", ";
	}
	document.getElementById("deleted_data").value = deleted_records;
	if (deleted_records == "")
		window.alert("您没有选择要删除的记录！");
	else if (window.confirm("确定要删除第 " + deleted_records.substr(0, deleted_records.length - 2) + " 条记录吗？") == true)
		document.getElementById("form_delete").submit();
}

function loadHandler()
{
	if (document.getElementsByName("checkbox").length > 0)
		document.getElementById("choose_all").disabled = false;
	else
		document.getElementById("choose_all").disabled = true;
}
//-->
</script>
</head>

<body onload="initialize();loadHandler();">
<jsp:useBean id="database" class="DatabaseConnection.DatabaseConnection" />
<%@include file="connect_database.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String database_name = request.getParameter("database");
String table_name = request.getParameter("table");
String sql_columns = "show columns from " + table_name + ";";
ResultSet resultset_columns = database.executeQuery(sql_columns, database_name);
String [] columns = null;
if (resultset_columns != null)
{
	String columns_string = "";
	while (resultset_columns.next() == true)
	{
		columns_string = columns_string + resultset_columns.getString("Field") + ",";
	}
	columns = columns_string.split(",");
}
String sql_records = "select * from " + table_name + ";";
ResultSet resultset_records = database.executeQuery(sql_records, database_name);
if (resultset_columns != null && resultset_records != null)
{
%>
<div style="padding-top:20px" />
<div align="center"><font size="+1"><strong>数据表 <%= table_name %> 中的记录</strong></font></div>
<br />
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#B1C8D7">
  <tr align="center" bgcolor="#F4F4FF">
    <td width="8%" height="24"><strong>选择</strong></td>
    <%
	for (int i = 0; i < columns.length; i++)
	{
	%>
	<td height="24"><strong><%= columns[i] %></strong></td>
	<%
	}
	%>
  </tr>
  <%
  int index = 0;
  while (resultset_records.next() == true)
  {
  %>
  <tr align="center" bgcolor="#FFFFFF" id="tr_<%= index %>" onmouseover="mouseOverHandler('<%= index %>');" onmouseout="mouseOutHandler('<%= index %>');" onclick="clickHandler('<%= index %>');">
    <td height="24"><input name="checkbox" type="checkbox" id="checkbox_<%= index %>" onclick="clickHandler('<%= index %>');" /></td>
    <%
	for (int i = 0; i < columns.length; i++)
	{
	%>
	<td height="24"><%= resultset_records.getString(columns[i]) %></td>
	<%
	}
	%>
  </tr>
  <%
  index++;
  }
  if (index == 0)
  {
  %>
  <tr align="center" bgcolor="#FFFFFF" id="tr_[empty]" onmouseover="mouseOverHandler('[empty]');" onmouseout="mouseOutHandler('[empty]');">
    <td height="24"><input name="checkbox_no_record" type="checkbox" id="checkbox_no_record" disabled="disabled" /></td>
    <td height="24" colspan="<%= columns.length %>">（没有记录）</td>
  </tr>
  <%
  }
  %>
  <tr align="center" bgcolor="#F4F4FF">
    <td height="24">
      <input name="choose_all" type="checkbox" id="choose_all" onclick="chooseAllHandler();" />
      <label for="choose_all"><strong>全选</strong></label>
    </td>
    <td height="24" colspan="<%= columns.length %>">
      <input name="insert" type="button" class="input_button" id="insert" onclick="window.location.href='records_insert.jsp?database=<%= database_name %>&table=<%= table_name %>';" value="添加" />
      　
      <input name="delete" type="button" class="input_button" id="delete" onclick="deleteHandler();" value="删除" />
      　
      <input name="back" type="button" class="input_button" id="back" onclick="window.location.href='tables.jsp?database=<%= database_name %>';" value="返回" />
    </td>
  </tr>
</table>
<form id="form_delete" name="form_delete" method="post" action="records_delete_do.jsp?database=<%= database_name %>&table=<%= table_name %>">
  <input name="deleted_data" type="hidden" id="deleted_data" />
</form>
<%
}
else
{
%>
<script type="text/javascript">
<!--
	window.alert("打开数据表 <%= table_name %> 失败！");
	window.location.href = "tables.jsp?database=<%= database_name %>";
//-->
</script>
<%
}
if (resultset_columns != null)
	resultset_columns.close();
if (resultset_records != null)
	resultset_records.close();
database.closeConnection();
%>
</body>
</html>